classdef MultiDeviceSyncModule < EquipmentControl.ZI.ziServer.ServerModules.AbstractServerModule
    %MULTIDEVICESYNCMODULE �˴���ʾ�йش����ժҪ
    %   �˴���ʾ��ϸ˵��
    
    properties
        group
    end
    
    methods
        function obj = MultiDeviceSyncModule(server, name)
            %MULTIDEVICESYNCMODULE ��������ʵ��
            %   �˴���ʾ��ϸ˵��
            obj@EquipmentControl.ZI.ziServer.ServerModules.AbstractServerModule(server, name, 'multiDeviceSyncModule');
            
        end
        
        function obj = initialize(obj, varargin)
            p=inputParser;
            p.addParameter('group', 0, @(x) x>0);
            p.parse(varargin{:});
            
            L = log4m.getLogger;
            if ~isempty(obj.module_handle)
                L.info('AbstractServerModule::initialize', ...
                   sprintf('Old module handle [%d] of [%s] Module [%s] cleared.',obj.module_handle, obj.module_root, obj.module_name));
                ziDAQ('clear', obj.module_handle);
            end
            
            obj.module_handle = ziDAQ('multiDeviceSyncModule');
            L.info('MultiDeviceSyncModule::MultiDeviceSyncModule', sprintf('Module Handle [%d] created.',obj.module_handle));
            
            obj.group = p.Results.group;
            obj.setModuleSettingValue(ziSettingsMultiDeviceSyncModule.devices, strjoin(obj.server.deviceSerials, ','));
            obj.setModuleSettingValue(ziSettingsMultiDeviceSyncModule.group, obj.group);

            obj.subscribed_path = {};
            obj.updateModuleSettings();
        end
    end
    
    %%
    methods
        function status = run(obj)
            L = log4m.getLogger;
            
            obj.sync();
            obj.execute();            
            obj.setModuleSettingValue(ziSettingsMultiDeviceSyncModule.start, 1);
            
            tic; start = toc; timeout = 20;
            status = obj.getStatus();
            isTimeOut = false;
            while status ~=2 && ~isTimeOut
                pause(0.2)
                status = obj.getStatus();
                if (toc - start) > timeout
                    isTimeOut = true;
                end
            end
            
            if status == 2 && ~isTimeOut
                L.info('MultiDeviceSyncModule::run', 'Devices successfully synchronized.');
            elseif status == -1
                L.error('MultiDeviceSyncModule::run', 'Error during device sync'); 
            elseif isTimeOut
                L.error('MultiDeviceSyncModule::run', 'Timeout during device sync');
            else
                error('Unknown error occured [%d].', status);
            end
        end
        
        function obj = phasesync(obj)
            L = log4m.getLogger;
            L.info('MultiDeviceSyncModule::phasesync', 'Synchronise oscillator phases.');
            obj.setModuleSettingValue(ziSettingsMultiDeviceSyncModule.phasesync, 1);
        end
        
        function status = getStatus(obj)
            status = ziDAQ('getInt', sprintf('/ZI/MDS/GROUPS/%d/status', obj.group));
        end
        
        function obj = setup(obj, varargin)
        end
    end
end

